const row = 10 // 每次获取10条新闻
var page = 0  // 当前新闻页

Page({
  /**
   * 页面的初始数据
   */
  data: {
    nickName: "",
    headSrc: "",
    sex: "",
    openId: "",
    isLogin: false,
    newsList: [],
  },

  // 获取用户信息
  getMyInfo: function(e) {
    // console.log(e.detail.userInfo)
    let info = e.detail.userInfo
    this.setData({
      headSrc: info.avatarUrl,
      nickName: info.nickName,
      sex: info.gender,
      isLogin: true
    })

    // 获取openid
    let that  = this
    let isSuperUser = false
    wx.cloud.callFunction({
      name: "getOpenId",
      success: res => {
        console.log("获取openId成功", res)
        that.setData({
          openId: res.result.openid
        }),

        // 判断是否为管理员
        wx.cloud.callFunction({
          name: "getSuperUserList",
          success: sres => {
            console.log("调用云函数获取管理者成功", sres)

            for(let i = 0; i < sres.result.data.length; ++i) {
              if (sres.result.data[i].openId == res.result.openid) {
                isSuperUser = true
                wx.redirectTo({
                  url: '/pages/superUser/superUser?nickName=' + that.data.nickName + "&headSrc=" + that.data.headSrc + "&openId=" + that.data.openId
                })
              }
            }
          },
          fail: sres => {
            console.log("调用云函数获取管理者失败", sres)
          }
        })

        
        // 如果不是管理员账号，则判断是不是新用户，是新用户则导入到个人资料页面
        if(isSuperUser == false) {
          wx.cloud.callFunction({
            name: "getUserByOpenId",
            data: {
              openId: that.data.openId
            },
            success: res => {
              console.log("调用云函数获取用户信息成功", res)
              // 如果是新用户
              if(res.result.data.length == 0) {
                wx.showToast({
                  title: '正在跳转',
                  duration: 1500,
                })
                setTimeout(function () {
                  wx.navigateTo({
                    url: '/pages/me/me?headSrc=' + that.data.headSrc + "&sex=" + that.data.sex + "&openId=" + that.data.openId
                  })
                }, 1500);
              }
            },
            fail: res => {
              console.log("调用云函数获取用户信息失败", res)
            }
          })
        }
      },
      fail: res => {
        console.log("获取openId失败", res)
      }
    })
  },

  // 点击个人资料跳转到我的详细页面
  goToMe:function() {
    if(this.data.isLogin == false) {
      wx.showToast({
        title: '请先登录',
        image: '/images/unhappy.png'
      })
    } else {
      let that = this
      wx.navigateTo({
        url: '/pages/me/me?headSrc=' + that.data.headSrc + "&sex=" + that.data.sex + "&openId=" + that.data.openId
      })
    }
  },

  // 点击成绩查询跳转到报比赛记录页面
  goToGameRecord: function () {
    if (this.data.isLogin == false) {
      wx.showToast({
        title: '请先登录',
        image: '/images/unhappy.png'
      })
    } else {
      let that = this
      wx.navigateTo({
        url: '/pages/gameRecord/gameRecord?openId=' + this.data.openId
      })
    }
  },

  // 点击报名参赛跳转到报名页面
  goToSignUp: function () {
    if (this.data.isLogin == false) {
      wx.showToast({
        title: '请先登录',
        image: '/images/unhappy.png'
      })
    } else {
      let that = this
      wx.navigateTo({
        url: '/pages/signUp/signUp'
      })
    }
  },

  // 点击报名查询跳转到报名查询页面
  goToSignUpAsk: function () {
    if (this.data.isLogin == false) {
      wx.showToast({
        title: '请先登录',
        image: '/images/unhappy.png'
      })
    } else {
      let that = this
      wx.navigateTo({
        url: '/pages/signUpAsk/signUpAsk?openId=' + this.data.openId
      })
    }
  },

  // 点击新闻跳转到对应新闻详情页
  goToNewsDetail: function (e) {
    //获取携带data-id的数据
    let id = e.currentTarget.dataset.id
    // console.log(e)
    //携带新闻ID进行页面跳转
    wx.navigateTo({
      url: '../newsDetail/newsDetail?id=' + id,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    // 从管理页面跳转来时设置为已登录状态
    let pages = getCurrentPages();
    if(pages.length > 1) {
      let prevpage = pages[pages.length - 2];
      if (prevpage.route == "pages/superUser/superUser") {
        this.setData({
          isLogin: true,
          headSrc: options.headSrc,
          openId: options.openId,
          nickName: options.nickName
        })
      }
    }
    // console.log(this.data.isLogin)
    let that = this
    wx.cloud.callFunction({
      name: "getNewsList",
      data: {
        row: row,
        page: page
      },
      success: res => {
        console.log("调用云函数获取新闻列表成功", res)
        that.setData({
          newsList: res.result.data
        })
      },
      fail: res => {
        console.log("调用云函数获取新闻列表失败", res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    page++
    let that = this
    wx.cloud.callFunction({
      name: "getNewsList",
      data: {
        row: row,
        page: page
      },
      success: res => {
        console.log("onReachBottom调用云函数获取新闻列表成功", res)
        //获取原有的新闻纪录
        let old_data = that.data.newsList
        //获取新页面的新闻纪录
        let new_data = res.result.data
        that.setData({
          newsList: old_data.concat(new_data)
        })
      },
      fail: res => {
        console.log("onReachBottom调用云函数获取新闻列表失败", res)
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})